package com.gherasoft.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import com.gherasoft.adapter.RunCheckListAdapter;
import com.gherasoft.model.CheckListItem;
import com.gherasoft.model.CheckListModel;

public class RunListActivity extends Activity
{
    private CheckListModel model;
    private RunCheckListAdapter adapter;
    private TextView listNameTextView;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.run_check_list);

        Intent intent = getIntent();
        int index = intent.getIntExtra("model_index", -1);

        this.model = MainActivity.getModel(index);
        this.adapter = new RunCheckListAdapter(this, this.model);
        this.model.setAdapter(this.adapter);

        ListView listView = (ListView)findViewById(R.id.mainListView);
        ImageButton editButton = (ImageButton) findViewById(R.id.listEditButton);
        this.listNameTextView = (TextView) findViewById(R.id.listNameTextView);

        this.listNameTextView.setText(this.model.getListName());

        listView.setAdapter(this.adapter);
        listView.setOnItemLongClickListener(new OnItemLongClickListener());
        listView.setOnItemClickListener(new OnItemClickListener());

        editButton.setOnClickListener(new OnEditButtonClickListener());
    }

    @Override
    public void onStop()
    {
        super.onStop();
        this.model.setAdapter(null);
    }

    @Override
    public void onResume()
    {
        super.onResume();
        this.adapter.notifyDataSetChanged();
        this.listNameTextView.setText(this.model.getListName());
        this.model.setAdapter(this.adapter);
    }

    private class OnEditButtonClickListener implements View.OnClickListener
    {
        @Override
        public void onClick(View view)
        {
            Intent intent = new Intent(RunListActivity.this, EditListActivity.class);
            int index = MainActivity.findModelIndex(RunListActivity.this.model);
            intent.putExtra("model_index", index);
            startActivity(intent);
        }
    }

    private class OnItemClickListener implements AdapterView.OnItemClickListener
    {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int index, long l)
        {
            CheckListItem item = (CheckListItem)RunListActivity.this.adapter.getItem(index);

            if (!item.isFailed())
            {
                RunListActivity.this.model.setItemChecked(index, !item.isChecked());
                RunListActivity.this.model.saveList(RunListActivity.this.getApplicationInfo().dataDir);
            }
        }
    }

    private class OnItemLongClickListener implements AdapterView.OnItemLongClickListener
    {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int index, long l)
        {
            CheckListItem item = (CheckListItem)RunListActivity.this.adapter.getItem(index);
            RunListActivity.this.model.setItemFailed(index, !item.isFailed());
            RunListActivity.this.model.saveList(RunListActivity.this.getApplicationInfo().dataDir);
            RunListActivity.this.model.setItemChecked(index, false);
            return true;
        }
    }
}
